from plot_type import *
import imagecache
from link import Link
from border import Fence, Border
from item import Item

class Plot:    
    def __init__(self, type=SOIL):
        self.type = type
        self.watered = False
        self.plant = None
        self.contents = list()
    
    # vestige of a time when plot was so much greater
    def plantable(self):
        return ((self.type == SOIL) and (self.plant == None))
    
    # returns true iff all contents are walkable
    def walkable(self):
        if self.type == BLANK:
            w =  False
            for c in self.contents:
                w |= c.walkable()
            return w
        
        w = True
        for c in self.contents:
            w &= c.walkable()
        return w
    
    # remove the plant, and return the price of the plant
    def harvest(self):
        price = 0
        if self.plant:
            price = self.plant.get_price()
            self.plant = None
        return price
    
    # age the plant, if a plant exists
    def inc_age(self):
        self.watered = False
        if self.plant:
            self.plant.inc_age()
    
    # will set a plant in this plot iff the plot is plantable
    def set_plant(self, plant):
        if self.plantable():
            self.plant = plant
            return True
        return False    
    
    # set this plot and its plant to be watered
    def set_watered(self):
        self.watered = True
        if(self.plant):
            self.plant.set_watered()
    
    # return a tile of this and its plant and all its contents
    def get_tile(self):
        # get ground type
        base_image = None
        if(self.type == SOIL):
            if(self.watered):
                base_image = imagecache.get_image("soil-watered")
            else:
                base_image = imagecache.get_image("soil-unwatered")
        elif(self.type == STONE):
            base_image = imagecache.get_image("stone")
        elif(self.type == PATH):
            base_image = imagecache.get_image("path")
        elif(self.type == GRASS):
            base_image = imagecache.get_image("grass")
        elif(self.type == FLOOR):
            base_image = imagecache.get_image("floor")
        elif(self.type == BLANK):
            base_image = imagecache.get_image("blank")
        
        base_image = base_image.copy() # copy so it doesn't alter the original cached image
        
        # get any contents
        for content in self.contents:
            base_image.blit(content.get_image(), (0,0))
        
        # add plant
        if(self.plant):
            base_image.blit(self.plant.get_image(), (0,0))
        
        return base_image
    
    def __str__(self):
        if(self.plant):
            return str(self.plant)
        elif(self.type == SOIL):
            return "G"
        elif(self.type == STONE):
            return "S"
        elif(self.type == PATH):
            return "P"
        else:
            return "?"
